package org.flashbrighton.as3bots.views.ui.scores 
{
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.Sprite;
	import org.flashbrighton.as3bots.models.BotModel;
	import org.flashbrighton.as3bots.components.HealthComponent;
	
	
	public class HealthBar extends Sprite
	{
		private static var START_COLOUR:int = 0xFFEE00;
		private static var END_COLOUR:int = 0xFF0000;
		private static var BAR_WIDTH:int = 100;
		private static var BAR_HEIGHT:int = 15;
		
		private var _botModel:BotModel;
		private var _bar:Shape;
		private var _mask:Shape;
		
		public function HealthBar(botModel:BotModel) 
		{
			_botModel = botModel;
			init();
		}
		
		
		public function update():void 
		{
			var health:Number = _botModel.energy / HealthComponent.MAX_ENERGY;
			if (health < 0) health = 0;
			_mask.scaleX = health;
		}
		
		
		private function init():void
		{			
			_mask = drawBar();
			_mask.x = 1;
			_mask.y = 1;
			addChild(_mask);
			
			_bar = drawBar();
			_bar.x = 1;
			_bar.y = 1;
			_bar.mask = _mask;
			addChild(_bar);
			
			
			// Border
			graphics.lineStyle(1, 0xFFFFFF);
			graphics.drawRect(0, 0, BAR_WIDTH, BAR_HEIGHT);			
		}
		
		
		private function drawBar():Shape 
		{
			var s:Shape = new Shape();
			s.graphics.beginGradientFill(GradientType.LINEAR, [END_COLOUR, START_COLOUR], [1.0, 1.0], [100, 255]);
			s.graphics.drawRect(0, 0, BAR_WIDTH - 1, BAR_HEIGHT - 1);
			s.graphics.endFill();
			
			return s;
		}
		
	}

}